<template>
	<view class="Wallpaper_classify">
		<navComponent :options="options" @SEL="selectSEL"></navComponent>
		<wallPaper :wallpaperList="wallpaperList"></wallPaper>
	</view>
</template>

<script>
	import navComponent from '../../components/nav_component/nav_component.vue'
	import wallPaper from '../../components/my_wallpaper/my_wallpaper.vue'
	export default {
		data() {
			return {
				options: [],
				wallpaperList: [],
				queryObj: {
					classify: '全部',
					limit: 10,
					page: 1
				},
				total: 0,
				isLoading: false,
				 
				sel: 0
			};
		},
		components: {
			navComponent,
			wallPaper
		},
		onLoad(options) {
			this.getOptions()
			this.getWallpaperList()
		},
		onReachBottom() {
			if (this.queryObj.page * this.queryObj.limit >= this.total) return uni.showToast({
				title: '数据已经空啦',
				icon: 'success'
			})
			if (this.isLoading) return
			this.queryObj.page += 1
			this.getWallpaperList()
		},
		onPullDownRefresh() {
			this.total = 0
			this.isLoading = false
			this.wallpaperList = []
			this.queryObj.page = 1
			this.getWallpaperList(() => {
				uni.stopPullDownRefresh()
			})
		},
		methods: {
			async getOptions() {
				const {
					data: res
				} = await this.$myRequest({
					url: '/my/options?classify=壁纸'
				})
				this.options = res.data
				for (let i = 0; i < this.options.length; i++) {
					this.options[i].id = i + 1
				}
			},
			
			async getWallpaperList(cb) {
				this.isLoading = true
				uni.showLoading({
					title: '数据加载中'
				})
	
				const {
					data: res
				} = await this.$myRequest({
					url: '/my/wallpapers/getwallpaper',
					data: this.queryObj
				})
				if (res.status != 0) {
					return uni.showToast({
						title: '请求失败可能是参数错误',
						duration: 1500,
						icon: 'none'
					})
				}
				cb && cb()
				uni.hideLoading()
				this.wallpaperList = [...this.wallpaperList, ...res.data]
				this.total = res.total
				this.isLoading = false
			},
			
			selectSEL(val) {
				this.sel = val
				// this.sel = parseInt(this.sel)
				for (let i = 0; i < this.options.length; i++) {
					if (this.sel == this.options[i].id) {
						this.queryObj.classify = this.options[i].classify
						this.queryObj.page = 1
						this.wallpaperList = []
						this.getWallpaperList()
						break
					}
					if (this.sel == 0) {
						this.queryObj.classify = '全部'
						this.queryObj.page = 1
						this.wallpaperList = []
						this.getWallpaperList()
						break
					}
				}
				
			}
		}
	}
</script>

<style lang="scss">

</style>
